# code to estimate penalized inverse covariance between scaled average document scores
library(data.table)
library(tidyverse)
library(parrot)
library(bootnet)

source("pundits_functions.R")

set.seed(1111)

pundits.agg.imp0 <- readr::read_csv("../data/pundits_parrot_aggregated_imp0.csv")

message("Running everything (with party) net...")
pundits_net_withparty <- bootnet::bootnet(pundits.agg.imp0[,2:ncol(pundits.agg.imp0)],
                                default = "EBICglasso",
                                verbose = TRUE,
                                weighted = TRUE,
                                signed = TRUE,
                                directed = FALSE,
                                nCores = 10)

save(pundits_net_withparty, file = "../output/aggregate_nets/pundits_parrot_net_all.RData")


message("Running everything but partisanship net...")
pundits_net <- bootnet::bootnet(pundits.agg.imp0[,2:ncol(pundits.agg.imp0)] %>%
                                           dplyr::select(-ends_with("_democrat"),
                                                         -ends_with("_republican")) %>%
                                             data.frame(),
                                default = "EBICglasso",
                                verbose = TRUE,
                                weighted = TRUE,
                                signed = TRUE,
                                directed = FALSE,
                                nCores = 10)
save(pundits_net, file = "../output/aggregate_nets/pundits_parrot_net_noparty.RData")


message("Running operational net...")
pundits_net_operational <- bootnet::bootnet(pundits.agg.imp0 %>%
                                           dplyr::select(ends_with("_climate"),
                                                         ends_with("_guns"),
                                                         ends_with("_health_care_insurance"),
                                                         ends_with("_immigration"),
                                                         ends_with("_iran"),
                                                         ends_with("_reproductive_health"),
                                                         ends_with("_taxes_spending"),
                                                         ends_with("_trade")) %>%
                                             data.frame(),
                                default = "EBICglasso",
                                verbose = TRUE,
                                weighted = TRUE,
                                signed = TRUE,
                                directed = FALSE,
                                nCores = 10)

save(pundits_net_operational, file = "../output/aggregate_nets/pundits_parrot_net_operational.RData")

message("Running symbolic net...")
pundits_net_symbolic <- bootnet::bootnet(pundits.agg.imp0 %>%
                                              dplyr::select(ends_with("_conservative"),
                                                            ends_with("_liberal"),
                                                            ends_with("_progressive"),
                                                            ends_with("_far_left"),
                                                            ends_with("_far_right")) %>%
                                           data.frame(),
                                            default = "EBICglasso",
                                            verbose = TRUE,
                                            weighted = TRUE,
                                            signed = TRUE,
                                            directed = FALSE,
                                            nCores = 10)

save(pundits_net_symbolic, file = "../output/aggregate_nets/pundits_parrot_net_symbolic.RData")

message("Running non-policy net...")
pundits_net_nonpolicy <- bootnet::bootnet(pundits.agg.imp0 %>%
                                           dplyr::select(ends_with("_class"),
                                                         ends_with("_gender"),
                                                         ends_with("_israel"),
                                                         ends_with("_lgbt"),
                                                         ends_with("_mueller"),
                                                         ends_with("_race")) %>%
                                           data.frame(),
                                         default = "EBICglasso",
                                         verbose = TRUE,
                                         weighted = TRUE,
                                         signed = TRUE,
                                         directed = FALSE,
                                         nCores = 10)

save(pundits_net_nonpolicy, file = "../output/aggregate_nets/pundits_parrot_net_nonpolicy.RData")